package myky.com.doctorapp.utils;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;

import myky.com.doctorapp.entity.LocalDataEntity;
import myky.com.doctorapp.entity.LocalUserEntity;
import myky.com.doctorapp.base.CMApplication;

/**
 * Created by 61904 on 2016/11/22.
 */


//sharedpre小型缓存工具类
public class SharedPre {
    private static SharedPre sharedPre;
    public static LocalUserEntity user_bean;//用户信息
    public final static String SP_LoginUserName="SP_LoginUserName";
    public final static String SP_UserName="SP_UserName";
    public final static String SP_PassWord="SP_PassWord";
    public final static String SP_HeadPhoto="SP_HeadPhoto";
    public final static String SP_NickName="SP_NickName";
    public final static String SP_Phone="SP_Phone";
    public final static String SP_USER_BEAN="SP_user_bean";
    public final static String SP_USER_STATE="SP_user_state";
    public final static String SP_CHECK_BOX="SP_check_box";
    public final static String IS_LOGIN="IS_LOGIN";
    public final static String NO_LOGIN="NO_LOGIN";

    public final static String DEVICE_TOKEN="DEVICE_TOKEN";


    public final static String SP_LOCAL_DATA="SP_LOCAL_DATA";//本地数据集合
    public final static String SP_IS_AUTHOR="SP_IS_AUTHOR";//是否授权-第一次进入我的病历
    public final static String SP_AUTHOR_DATE="SP_AUTHOR_DATE";//是否授权-第一次进入我的病历-操作时间
    public final static String SP_IS_FIRST_OPEN="SP_IS_FIRST_OPEN";//是否第一次进入我的医生列表
    public final static String SP_CASENUM_DATE="SP_CASENUM_DATE";//首页病历用户量计数日期本地保存


    public SharedPre() {
        super();
    }

    public static SharedPre getInstance(){

        if (sharedPre==null){
        sharedPre=new SharedPre();
        }
        return sharedPre;
    }
    public static void setSharedinfo(Context context, String name, String value){

        context.getSharedPreferences(name, Context.MODE_PRIVATE).edit().putString(name,value).commit();

    }
    public static String getSharedinfo(Context context, String name){
        String value=null;
               value= context.getSharedPreferences(name, Context.MODE_PRIVATE).getString(name,null);

        return value;
    }


    //保存本地数据
    public static void setLocalData(String name, Object value){

        String localData=getSharedinfo(CMApplication.getInstance().getApplicationContext(),SP_LOCAL_DATA);
        LoggerUtils.i("localData_set",localData);
        LocalDataEntity localDataBean=null;
        if (localData==null){
            localDataBean=new LocalDataEntity();
        }else {
            localDataBean=GsonUtil.GsonToBean(localData,LocalDataEntity.class);
        }

        switch (name){

            case SP_IS_AUTHOR:
                localDataBean.setAuthor((Boolean) value);
                break;
            case SP_AUTHOR_DATE:
                localDataBean.setAuthor_date((String) value);
                break;
            case SP_IS_FIRST_OPEN:
                localDataBean.setFirstOpen((Boolean) value);
                break;
            case SP_CASENUM_DATE:
                localDataBean.setCaseNum_date((String) value);
                break;
        }

        setSharedinfo(CMApplication.getInstance().getApplicationContext(),SP_LOCAL_DATA,GsonUtil.GsonString(localDataBean));
    }

    //获取所保存数据
    public static LocalDataEntity getLocalData(){

        String localData=getSharedinfo(CMApplication.getInstance().getApplicationContext(),SP_LOCAL_DATA);
        LoggerUtils.i("localData_get",localData);
        LocalDataEntity localDataBean=null;


            localDataBean=GsonUtil.GsonToBean(localData,LocalDataEntity.class);

        return localDataBean;
    }

    //获取单个元素数据
    public static Object getLocalData(String name){

        String localData=getSharedinfo(CMApplication.getInstance().getApplicationContext(),SP_LOCAL_DATA);
        LoggerUtils.i("localData_get",localData);
        LocalDataEntity localDataBean=null;
        Object value=null;
        if (localData==null){
            localDataBean=new LocalDataEntity();
        }else {
            localDataBean = GsonUtil.GsonToBean(localData,LocalDataEntity.class);
        }

        switch (name){

            case SP_IS_AUTHOR:
                value= localDataBean.isAuthor();
                break;
            case SP_AUTHOR_DATE:
                value= localDataBean.getAuthor_date();
                break;
            case SP_IS_FIRST_OPEN:
                value= localDataBean.isFirstOpen();
                break;
            case SP_CASENUM_DATE:
                value= localDataBean.getCaseNum_date();
                break;
        }
        return value;
    }
    //获取本地个人信息
    public static LocalUserEntity getUser_bean(Context context){
        String userInfo=getSharedinfo(context.getApplicationContext(),SP_USER_BEAN);
        Log.i("out","SP_USER_BEAN。。"+userInfo);
        if (userInfo!=null){
            Gson gson=new Gson();
            user_bean=gson.fromJson(userInfo, LocalUserEntity.class);
        }
        return user_bean;
    }

    //SP保存用户个人信息
    public static void setUser_bean(Context context, String info, String userType) {
        LocalUserEntity user_bean = null;
        if (getUser_bean(context)==null){
            user_bean=new LocalUserEntity();
        }else {
            user_bean=getUser_bean(context);
        }
        Gson gson=new Gson();
        switch (userType){
            case SP_LoginUserName:
                user_bean.setLoginUserName(info);
                break;
            case SP_UserName:
                user_bean.setUserName(info);
                break;
            case SP_PassWord:
                user_bean.setPassWord(info);
                break;
            case SP_NickName:
                user_bean.setNickName(info);
                break;
            case SP_HeadPhoto:
                user_bean.setHeadPhoto(info);
                break;
            case SP_Phone:
                user_bean.setPhone(info);
                break;
        }
        Log.i("out","setSharedinfo.."+gson.toJson(user_bean));
        setSharedinfo(context,SP_USER_BEAN,gson.toJson(user_bean));
    }


    //清空用户个人信息
    public static void clearUser_Bean(Context context){
        LocalUserEntity user_bean=new LocalUserEntity();
        Gson gson=new Gson();
        setSharedinfo(context,SP_USER_BEAN,gson.toJson(user_bean));
    }
}
